package com.baic.ibase.controller.api.v1;


import com.baic.ibase.models.dao.Permissions;
import com.baic.ibase.models.req.common.ApiResponse;
import com.baic.ibase.models.req.common.IdRequest;
import com.baic.ibase.models.req.common.PageRequest;
import com.baic.ibase.models.req.permissions.CreatePermissionsReq;
import com.baic.ibase.models.req.permissions.DeletePermissionReq;
import com.baic.ibase.service.permissions.PermissionsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/api/v1/permissions")
public class PermissionController {

    @Autowired
    private PermissionsService permissionsService;

    /**
     * @api {POST} /api/v1/permissions/ListPermissions 查询权限列表
     * @apiVersion 1.0.0
     * @apiGroup PermissionController
     * @apiName ListPermissions()
     * @apiParam (请求体) {Number} pageNum
     * @apiParam (请求体) {Number} pageSize
     * @apiParam (请求体) {String} sort
     * @apiParam (请求体) {String} order
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"d2wzC","pageSize":4907,"sort":"BXn","pageNum":8709,"order":"S"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Array} data.records
     * @apiSuccess (响应结果) {Number} data.records.id
     * @apiSuccess (响应结果) {String} data.records.permissionName 权限名称
     * @apiSuccess (响应结果) {String} data.records.permissionCode 权限编码
     * @apiSuccess (响应结果) {String} data.records.description 描述
     * @apiSuccess (响应结果) {Object} data.records.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.records.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.records.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.records.updatedUserId 更新人
     * @apiSuccess (响应结果) {Number} data.total
     * @apiSuccess (响应结果) {Number} data.size
     * @apiSuccess (响应结果) {Number} data.current
     * @apiSuccess (响应结果) {Array} data.orders
     * @apiSuccess (响应结果) {String} data.orders.column
     * @apiSuccess (响应结果) {Boolean} data.orders.asc
     * @apiSuccess (响应结果) {Boolean} data.optimizeCountSql
     * @apiSuccess (响应结果) {Boolean} data.searchCount
     * @apiSuccess (响应结果) {Boolean} data.optimizeJoinOfCountSql
     * @apiSuccess (响应结果) {Number} data.maxLimit
     * @apiSuccess (响应结果) {String} data.countId
     * @apiSuccessExample 响应结果示例
     * {"traceId":"zISguUT3yD","code":1423,"data":{"total":1103,"current":3981,"size":254,"optimizeCountSql":true,"records":[{"createdUserId":1681,"description":"RLvpph","id":1465,"permissionCode":"8tfsR0w2","updatedUserId":2856,"permissionName":"ZbGKnZfQxm"}],"maxLimit":9617,"searchCount":false,"optimizeJoinOfCountSql":true,"orders":[{"asc":true,"column":"oeX8C"}],"countId":"uFhe"},"success":true,"message":"gKpQ","errors":"ZSRi0mWn"}
     */
    @PostMapping("/ListPermissions")
    public ApiResponse<Page<Permissions>> ListPermissions(@RequestBody PageRequest req) {
        Page<Permissions> page = permissionsService.page(new Page<>(req.getPageNum(), req.getPageSize()));
        return ApiResponse.ok(page);
    }

    /**
     * @api {POST} /api/v1/permissions/AddPermissions 添加权限
     * @apiVersion 1.0.0
     * @apiGroup PermissionController
     * @apiName AddPermissions()
     * @apiParam (请求体) {String} permissionName
     * @apiParam (请求体) {String} permissionCode
     * @apiParam (请求体) {String} description
     * @apiParamExample 请求体示例
     * {"description":"OIwrEkXSVu","permissionCode":"6YlF","permissionName":"1z55mgj"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.permissionName 权限名称
     * @apiSuccess (响应结果) {String} data.permissionCode 权限编码
     * @apiSuccess (响应结果) {String} data.description 描述
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"DA3USl2it","code":5417,"data":{"createdUserId":4598,"description":"ggS7tjS3U","id":6757,"permissionCode":"Ak9","updatedUserId":5722,"permissionName":"JVeuVrl1"},"success":true,"message":"PDUdkkvL","errors":"NEO41"}
     */
    @PostMapping("/AddPermissions")
    public ApiResponse<Permissions> AddPermissions(@RequestBody @Validated CreatePermissionsReq req) {
        Permissions permissions = new Permissions();
        permissions.setPermissionName(req.getPermissionName());
        permissions.setPermissionCode(req.getPermissionCode());
        permissions.setDescription(req.getDescription());
        permissions.setCreatedUserId(1L);
        permissions.setUpdatedUserId(1L);
        permissions.setCreatedTime(LocalDateTime.now());
        permissions.setUpdatedTime(LocalDateTime.now());
        if (permissionsService.save(permissions)) {
            return ApiResponse.ok(permissions);
        } else {
            return ApiResponse.error(500, "添加权限失败");
        }
    }

    /**
     * @api {POST} /api/v1/permissions/DeletePermissions 删除权限
     * @apiVersion 1.0.0
     * @apiGroup PermissionController
     * @apiName DeletePermissions()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"8jxn3P6rn","id":6459}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Boolean} data
     * @apiSuccessExample 响应结果示例
     * {"traceId":"7bmyrV1Neb","code":8518,"data":false,"success":false,"message":"XbFhGu5gst","errors":"w6aPsB"}
     */
    @PostMapping("/DeletePermissions")
    public ApiResponse<Boolean> DeletePermissions(@RequestBody DeletePermissionReq req) {
        if (permissionsService.removeById(req.getId())) {
            return ApiResponse.ok(true);
        } else {
            return ApiResponse.error(500, "删除权限失败");
        }
    }

    /**
     * @api {POST} /api/v1/permissions/GetPermissions 查询权限详情
     * @apiVersion 1.0.0
     * @apiGroup PermissionController
     * @apiName GetPermissions()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"gdKrPiFJ","id":5851}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.permissionName 权限名称
     * @apiSuccess (响应结果) {String} data.permissionCode 权限编码
     * @apiSuccess (响应结果) {String} data.description 描述
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"GHXFnE","code":5861,"data":{"createdUserId":8129,"description":"Dy3Yvllam","id":8619,"permissionCode":"QWvp6F3","updatedUserId":8647,"permissionName":"VSWWAfZ"},"success":false,"message":"Z","errors":"l2DmRyPkE9"}
     */
    @PostMapping("/GetPermissions")
    public ApiResponse<Permissions> GetPermissions(@RequestBody IdRequest req) {
        Permissions permissions = permissionsService.getById(req.getId());
        return ApiResponse.ok(permissions);
    }

    /**
     * @api {POST} /api/v1/permissions/UpdatePermissions 更新权限
     * @apiVersion 1.0.0
     * @apiGroup PermissionController
     * @apiName UpdatePermissions()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} permissionName 权限名称
     * @apiParam (请求体) {String} permissionCode 权限编码
     * @apiParam (请求体) {String} description 描述
     * @apiParam (请求体) {Object} createdTime 创建时间
     * @apiParam (请求体) {Object} updatedTime 更新时间
     * @apiParam (请求体) {Number} createdUserId 创建人
     * @apiParam (请求体) {Number} updatedUserId 更新人
     * @apiParamExample 请求体示例
     * {"createdUserId":5109,"description":"eiVX","id":8260,"permissionCode":"Ex08COkdCD","updatedUserId":3519,"permissionName":"85We"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.permissionName 权限名称
     * @apiSuccess (响应结果) {String} data.permissionCode 权限编码
     * @apiSuccess (响应结果) {String} data.description 描述
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"2nQ","code":1226,"data":{"createdUserId":5223,"description":"EmIfSfZ","id":5420,"permissionCode":"NEqRqbtXl","updatedUserId":5832,"permissionName":"Pbb7"},"success":true,"message":"TcN","errors":"M"}
     */
    @PostMapping("/UpdatePermissions")
    public ApiResponse<Permissions> UpdatePermissions(@RequestBody Permissions req) {
        req.setUpdatedUserId(1L);
        req.setUpdatedTime(LocalDateTime.now());

        if (permissionsService.updateById(req)) {
            return ApiResponse.ok(req);
        } else {
            return ApiResponse.error(500, "更新权限失败");
        }
    }

}
